# -*- coding: utf-8 -*-
'''
Created on 30-09-2012

@author: LONG HOANG GIANG
'''

from urlparse import urljoin
import datetime
import os
import re
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import sqlite3 as db
from CrawlerLib import commonlib, html2text
from CrawlerLib.AESCipher import AESCipher

def get_list_truyen():
    print '> Get list story'
    url_format = 'http://alobooks.vn/162/trang-van/truyen-ngan-hay.aspx?page={0}'
    stories = commonlib.pickle_load('truyennganhaylist', __file__)
    if stories == None: 
        stories = []
        for page in range(0,14):
            url = url_format.format(page)
            tree = commonlib.loadweb(url).build_tree()
            for item in tree.xpath("//div[@class='view-content']/div[contains(@class, 'views-row')]//span[@class='field-content']/a"):
                title = commonlib.stringify(item)
                link = item.get('href', '')
                if link == '': continue
                link = urljoin(url, link)
                stories.append({'title': title, 'url': link})
        commonlib.pickle_dump('truyennganhaylist', stories, __file__)
    return stories

def get_detail_story(item):
    print '> get detail of {0}'.format(item['title'])
    url = item['url']
    tree = commonlib.loadweb(url).build_tree()
    contentNode = tree.xpath("//div[@class='content node-article']")
    html = '''<b>{0}</b> <br /><br /><br />'''.format(item['title'])
    html += html2text.html2text(commonlib.Etree.tohtml(contentNode[0]), url, download=commonlib.download_image, path='/hehe/')
    html = re.sub(r"\n", "<br />", html)
    return html

def createTable(c):
    c.execute("DROP TABLE IF EXISTS tblChapters")
    c.execute("DROP TABLE IF EXISTS tblDetail")
    c.execute("CREATE TABLE tblChapters(`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `title` TEXT)")
    c.execute("CREATE INDEX chapter_idx ON tblChapters(id)")
    c.execute("CREATE TABLE tblDetail(`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `chapterId` INTEGER, `detail` TEXT)")
    c.execute("CREATE INDEX detail_idx ON tblDetail(chapterId)")

def process():
    conn = db.connect(DBNAME)
    c = conn.cursor()
    createTable(c)
    stories = get_list_truyen()
    cipher = AESCipher('longhoanggiang')
    for story in stories:
        title = commonlib.toUpper(story['title'])
        title = cipher.encrypt(title)
        c.execute("INSERT INTO tblChapters(title) VALUES(?)", [title])
        detail = get_detail_story(story)
        c.execute("INSERT INTO tblDetail(chapterId, detail) VALUES(?, ?)", (c.lastrowid, detail))
    c.close()
    conn.commit()
    conn.close()

if __name__ == '__main__':
    DBNAME = 'story'
    process()
    print '>Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)